<?php

class DefaultController extends ControllerBackend
{
	public function actionIndex() {
        $this->render('/'.substr(strrchr(Yii::app()->controller->id,'/'),1).'/index');
	}
    
	public function actionLanguagesListX() {
        $assign['list']['rows'] = Languages::getList();
        $assign['list']['packLanguages'] = LanguagesPack::getList(1);
        $this->renderPartial('/'.substr(strrchr(Yii::app()->controller->id,'/'),1).'/LanguagesList', $assign);
	}
    	
    public function actionLanguagesList() { 
        $model = new Languages('search');
		$model->unsetAttributes();  // clear any default values                
		if(isset($_GET['Languages']))
			$model->attributes = $_GET['Languages'];
            
        // page size drop down changed
        if (isset($_GET['pageSize'])) {                        
            Yii::app()->user->setState('pageSize',(int)$_GET['pageSize']);                
            unset($_GET['pageSize']);
        }
        if (isset($_GET['languageKeyword'])) {                        
            Yii::app()->user->setState('languageKeyword',$_GET['languageKeyword']);                
            unset($_GET['languageKeyword']);
        }
        if (isset($_GET['languageGroup'])) {                        
            Yii::app()->user->setState('languageGroup',$_GET['languageGroup']);                
            unset($_GET['languageGroup']);
        }
        if (isset($_GET['languageModule'])) {                        
            Yii::app()->user->setState('languageModule',$_GET['languageModule']);                
            unset($_GET['languageModule']);
        }

		$columns = array();
        $columns[] = array(
            'class'=>'CCheckBoxColumn',            
            'id'=>'chkIds',
            'selectableRows'=>2,
            'htmlOptions'=>array('width'=>'20px', 'align'=>'center')
        );
        $columns[] = array (
            'header'=>'ID',
            'value'=>'$data->id',
            'htmlOptions'=>array('width'=>'20px')
        );
        $columns[] = array (
            'header'=>'Group',
            'value'=>'$data->lang_group',
            'htmlOptions'=>array('width'=>'70px')
        );
        $columns[] = array (
            'header'=>'Module',
            'value'=>'$data->module',
            'htmlOptions'=>array('width'=>'70px')
        );
        $columns[] = array (
            'header'=>'Key',
            'value'=>'$data->keyword'
        );
        foreach (LanguagesPack::getList(1) as $languagePack) {
            $columns[] = array (                
                'header'=>$languagePack->name,
                'type'=>'raw',
                'value'=>'$data->valueDecode->'.$languagePack->alias.'',
                'htmlOptions'=>array('width'=>'250px', 'class'=>'liveEditField', 'lang'=>$languagePack->alias, 'ondblclick'=>"js:ajaxGridLiveEdit('article-grid', this); return false")
            );
        }
        $columns[] = array (
            'header'=>'Active',
            'value'=>'$data->active',
            'htmlOptions'=>array('width'=>'30px')
        );
        $columns[] = array(
			'class'=>'CButtonColumn',
            'header'=>CHtml::dropDownList('pageSize',
                Yii::app()->user->getState('pageSize',20),
                array(2=>2,10=>10,20=>20,30=>30,40=>40,50=>50,100=>100,200=>200,500=>500,1000=>1000),
                array('onchange'=>"$.fn.yiiGridView.update('languages-grid',{ data:{pageSize: $(this).val() }})")
            ),
            'template'=>'{gridUpdate} {gridDelete}',
            'buttons' => array(
                'gridUpdate'=>array(
                    'label'=>'Update',
                    'url'=>'Yii::app()->createUrl("//languages/backend/default/updateKeyword", array("id"=>$data->id))',
                    'imageUrl'=>Yii::app()->theme->baseUrl.'/common/img/icon/icon_edit_16.png',                                        
                ),
                'gridDelete'=>array(
                    'label'=>'Delete',
                    'url'=>'Yii::app()->createUrl("//languages/backend/default/deleteKeyword", array("id"=>$data->id))',
                    'imageUrl'=>Yii::app()->theme->baseUrl.'/common/img/icon/icon_delete_16.png',                                        
                ),
            ),
		);
        
        //$this->layout = false;
        $this->renderPartial('/'.substr(strrchr(Yii::app()->controller->id,'/'),1).'/LanguagesListNew',array(
			'model'=>$model,
            'columns'=>$columns,
		));
	}
    
	public function actionLanguagesListTemp() { 
        $model = new Languages('search');
		$model->unsetAttributes();  // clear any default values                
		if(isset($_GET['Languages']))
			$model->attributes = $_GET['Languages'];
            
        // page size drop down changed
        if (isset($_GET['pageSize'])) {                        
            Yii::app()->user->setState('pageSize',(int)$_GET['pageSize']);                
            unset($_GET['pageSize']);
        }

		$columns = array();
        $columns[] = array(
            'class'=>'CCheckBoxColumn',
            'id'=>'chkIds',
            'selectableRows'=>2,
            'htmlOptions'=>array('width'=>'20px', 'align'=>'center')
        );
        $columns[] = array (
            'header'=>'ID',
            'value'=>'$data->id',
            'htmlOptions'=>array('width'=>'50px')
        );
        $columns[] = array (
            'header'=>'Group',
            'value'=>'$data->lang_group',
            'htmlOptions'=>array('width'=>'100px')
        );
        $columns[] = array (
            'header'=>'Module',
            'value'=>'$data->module',
            'htmlOptions'=>array('width'=>'100px')
        );
        $columns[] = array (
            'header'=>'Key',
            'value'=>'$data->keyword'
        );
        foreach (LanguagesPack::getList(1) as $languagePack) {
            $columns[] = array (
                'header'=>$languagePack->name,
                'type'=>'raw',
                'value'=>'$data->valueDecode->'.$languagePack->alias.'',
                'htmlOptions'=>array('width'=>'250px')
            );
        }
        $columns[] = array (
            'header'=>'Active',
            'value'=>'$data->active',
            'htmlOptions'=>array('width'=>'100px')
        );
        $columns[] = array(
			'class'=>'CButtonColumn',
            'header'=>CHtml::dropDownList('pageSize',
                Yii::app()->user->getState('pageSize',20),
                array(10=>10,20=>20,30=>30,40=>40,50=>50,100=>100,200=>200,500=>500,1000=>1000),
                array('onchange'=>"$.fn.yiiGridView.update('languages-grid',{ data:{pageSize: $(this).val() }})")
            ),
            'template'=>'{gridUpdate} {gridDelete}',
            'buttons' => array(
                'gridUpdate'=>array(
                    'label'=>'Update',
                    'url'=>'Yii::app()->createUrl("//languages/backend/default/updateKeyword", array("id"=>$data->id))',
                    'imageUrl'=>Yii::app()->theme->baseUrl.'/common/img/icon/icon_edit_16.png',                                        
                ),
                'gridDelete'=>array(
                    'label'=>'Delete',
                    'url'=>'Yii::app()->createUrl("//languages/backend/default/deleteKeyword", array("id"=>$data->id))',
                    'imageUrl'=>Yii::app()->theme->baseUrl.'/common/img/icon/icon_delete_16.png',                                        
                ),
            ),
		);
        
        $this->layout = false;
        $this->render('/'.substr(strrchr(Yii::app()->controller->id,'/'),1).'/admin',array(
			'model'=>$model,
            'columns'=>$columns,
		));
	}
    
	public function actionAddKeyword() {        
        if (Yii::app()->request->isAjaxRequest) {
            
            $assign['model'] = new Languages;
            $assign['list']['pack'] = LanguagesPack::getList(1);
            if (isset($_POST['Languages'])) {
                $model->attributes = $_POST['Languages']; // the la du roi
                $model->value = json_encode($_POST['value']);
                if ($model->validate()) {
                    $model->save();
                    Yii::app()->end();
                }
            }
            $this->renderPartial('/'.substr(strrchr(Yii::app()->controller->id,'/'),1).'/AddKeyword', $assign);
        }
	}
    
    public function actionUpdateKeyword() {
        echo 'Update Keywork';
    }
    
    public function actionDeleteKeyword() {
        echo 'Delete Keywork';
    }
    
    public function actionInsertKey() {
        if (Yii::app()->request->isAjaxRequest) {
            $model = new Languages;                
            if (isset($_POST['Languages'])) {
                $model->attributes = $_POST['Languages'];
                $model->value = json_encode($_POST['value']);
                if ($model->validate()) {
                    $model->save();
                }
            }
        }
    }
    
    public function actionUpdateKey() {
        foreach ($_POST['list'] as $key => $value) {
            $model = Languages::model()->findByPk((int)$key);
            $model->value = json_encode($value);
            $model->save();
        }
        //var_dump($_POST);
        
    }
}